﻿using System.Windows;
using System.Collections.Generic;
using DSPL.Services;
using DSPL.Contracts;
using DSPL.Common;
using System.Data;
using DSPL.Services.Services;
using DSPL.Contracts.Common;
using DSPL.Contracts.ViewModel;
using DSPL.Contracts.Command;
using DSPL.Finac.Common;
using System;
using System.Windows.Input;
using DSPL.Contracts.Interfaces;
using DSPL.Common.Classes;

namespace DSPL.Finac.ViewModel
{
    public class SalesReturnWithInvoiceViewModel : BaseViewModel
    {
        private bool isReferenceNoFocused;
        private bool isInvoiceNoFocused;
        private bool isCurrencyCodeFocused;
        private bool isSupplierCodeFocused;
        private bool isLocationCodeFocused;
        private bool isCostCodeFocused;
        private bool isExchangeRateFocused;
        private bool isTransactionDateFocused;
        private DataRow drItemRow;
        private ICommand _refreshCurrencyData;

        #region Define properties

        public bool IsInvoiceNoFocused
        {
            get { return isInvoiceNoFocused; }
            set
            {
                isInvoiceNoFocused = value;
                this.OnPropertyChanged("IsInvoiceNoFocused");
            }
        }

        public bool IsCurrencyCodeFocused
        {
            get { return isCurrencyCodeFocused; }
            set
            {
                isCurrencyCodeFocused = value;
                this.OnPropertyChanged("IsCurrencyCodeFocused");

            }
        }

        public bool IsSupplierCodeFocused
        {
            get { return isSupplierCodeFocused; }
            set
            {
                isSupplierCodeFocused = value;
                this.OnPropertyChanged("IsSupplierCodeFocused");

            }
        }

        public bool IsLocationCodeFocused
        {
            get { return isLocationCodeFocused; }
            set
            {
                isLocationCodeFocused = value;
                this.OnPropertyChanged("IsLocationCodeFocused");

            }
        }

        public bool IsCostCodeFocused
        {
            get { return isCostCodeFocused; }
            set
            {
                isCostCodeFocused = value;
                this.OnPropertyChanged("IsCostCodeFocused");

            }
        }

        public bool IsExchangeRateFocused
        {
            get { return isExchangeRateFocused; }
            set
            {
                isExchangeRateFocused = value;
                this.OnPropertyChanged("IsExchangeRateFocused");

            }
        }

        public bool IsTransactionDateFocused
        {
            get { return isTransactionDateFocused; }
            set
            {
                isTransactionDateFocused = value;
                this.OnPropertyChanged("IsTransactionDateFocused");

            }
        }

        public bool IsReferenceNoFocused
        {
            get { return isReferenceNoFocused; }
            set
            {
                isReferenceNoFocused = value;
                this.OnPropertyChanged("IsReferenceNoFocused");
            }
        }

        public DataRow ItemSelectedRow
        {
            get
            {
                return drItemRow;
            }
            set
            {
                drItemRow = value;
                OnPropertyChanged("ItemSelectedRow");
            }
        }

        #endregion


        #region Constructor

        public SalesReturnWithInvoiceViewModel()
            : base()
        {
            MessageTitle = "Sales Return With Invoice";
        }

        #endregion

        # region Commands

        public ICommand RefreshCurrencyData
        {
            get
            {
                if (this._refreshCurrencyData == null)
                {
                    this._refreshCurrencyData = new RelayCommand(RefreshCurrencyDataExecuted);
                }
                return this._refreshCurrencyData;
            }
        }
        #endregion

        /// <summary>
        /// Validate mandatory data before save
        /// </summary>
        /// <param name="obj"></param>
        protected override void PreDataValidateExecuted(object obj)
        {
            IsInvoiceNoFocused = false;
            IsCurrencyCodeFocused = false;
            IsSupplierCodeFocused = false;
            IsLocationCodeFocused = false;
            IsCostCodeFocused = false;
            IsExchangeRateFocused = false;
            IsTransactionDateFocused = false;
            IsReferenceNoFocused = false;

            var messageService = new MessageService();
            if (CurrentRow["RefNo"].ToString().Trim() == string.Empty)
            {
                messageService.ShowMessage(MessageText.ReferenceNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsReferenceNoFocused = true;
                this.IsValid = false;
                return;
            }

            if (Convert.ToString(CurrentRow["RefNo1"]) == string.Empty)
            {
                messageService.ShowMessage(MessageText.InvoiceNoNotEmpty, MessageTitle, MessageType.InfoOK);
                IsInvoiceNoFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["SupplierPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["SupplierPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.SupplierNotEmpty, MessageTitle, MessageType.InfoOK);
                IsSupplierCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["LocationPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["LocationPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.LocationNotEmpty, MessageTitle, MessageType.InfoOK);
                IsLocationCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CostPk"] == DBNull.Value || Convert.ToInt32(CurrentRow["CostPk"]) == 0)
            {
                messageService.ShowMessage(MessageText.CostCodeNotEmpty, MessageTitle, MessageType.InfoOK);
                IsCostCodeFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["CurRate"] == DBNull.Value || Convert.ToInt32(CurrentRow["CurRate"]) == 0)
            {
                messageService.ShowMessage(MessageText.CurrencyRateNotZero, MessageTitle, MessageType.InfoOK);
                IsExchangeRateFocused = true;
                this.IsValid = false;
                return;
            }

            if (CurrentRow["TxnDate"] == DBNull.Value)
            {
                messageService.ShowMessage(MessageText.TransactionDateNotEmpty, MessageTitle, MessageType.InfoOK);
                IsExchangeRateFocused = true;
                this.IsValid = false;
                return;
            }

            this.IsValid = true;
        }


        protected override void RefreshExternalDataObjectsExecueted(object obj)
        {
            base.RefreshExternalDataObjectsExecueted(obj);
            AddColumnsToChildTable();

        }

        private void RefreshCurrencyDataExecuted(object obj)
        {
            TxtCurrency txtCurrency = (TxtCurrency)obj;
            CurrentRow["CurRate"] = txtCurrency.CurrencyRate;
            this.OnPropertyChanged("CurrentRow");
        }



        private void AddColumnsToChildTable()
        {
            if (DtChildTransaction != null)
            {
                if (!DtChildTransaction.Columns.Contains("ItemCode"))
                {
                    DtChildTransaction.Columns.Add("ItemCode");
                }
                if (!DtChildTransaction.Columns.Contains("ItemName"))
                {
                    DtChildTransaction.Columns.Add("ItemName");
                }
                if (!DtChildTransaction.Columns.Contains("Qoh"))
                {
                    DtChildTransaction.Columns.Add("Qoh");
                }
            }
        }

        protected override void AddAddionalReportParametersExecuted(object obj)
        {
            base.AddAddionalReportParametersExecuted(obj);

            if (AdditionalReportParameters == null)
            {
                AdditionalReportParameters = new List<IReportParameters>();
            }
            AdditionalReportParameters.Add(new ReportParameters() { ParameterIndex = 8, ParameterName = "VatCompanyTaxNo", InputValue = GlobalVariables.VatCompanyTaxNo });
        }
    }
}


